package utils;

import com.sun.jmx.remote.internal.ArrayQueue;

import java.util.ArrayList;
import java.util.concurrent.LinkedBlockingQueue;

public class Test2 {
    class TreeNode {
        int val = 0;
        TreeNode left = null;
        TreeNode right = null;
        public TreeNode(int val) {
          this.val = val;
        }
    }

    public static void main(String[] args) {
        LinkedBlockingQueue<String> ss;
    }

    public ArrayList<ArrayList<Integer>> levelOrder (TreeNode root) {
        // write code here
        ArrayList<ArrayList<Integer>> result = new ArrayList<ArrayList<Integer>>();

        ArrayList<Integer> data1 = new ArrayList<>();
        data1.add(root.val);
        result.add(data1);

        ArrayList<TreeNode> nodes = new ArrayList<>();
        nodes.add(root);

        guangdu(nodes, result);

        return result;
    }

    private void guangdu(ArrayList<TreeNode> nodes, ArrayList<ArrayList<Integer>> result){
        ArrayList<TreeNode> nodeList = new ArrayList<>();
        ArrayList<Integer> integers = new ArrayList<>();

        for (TreeNode node: nodes){
            if (node.left != null){
                nodeList.add(node.left);
                integers.add(node.left.val);
            }

            if (node.right != null){
                nodeList.add(node.right);
                integers.add(node.right.val);
            }
        }

        result.add(integers);

        guangdu(nodeList, result);

    }
}
